<template>
  <div class="full flex-col">
    <div class="flex_1  full flex-row">
      <div class="flex_1" style="margin-right: 10px">
        <el-form label-position="top"
                 :model="data"
                 ref="form1"
                 style="margin: 10px; width: auto"
        >
          <el-form-item prop="userName" label="* 姓名">
            <el-input size="large" placeholder="请输入" v-model="data.userName"></el-input>
          </el-form-item>


          <el-form-item prop="position" label="* 岗位">
            <el-select v-model="data.position" size="large" class="w_full"
                       placeholder="请选择岗位">
              <el-option
                  v-for="(qtType, index) in data.listPosition"
                  :key="index"
                  :label="qtType.dictName"
                  :value="qtType.dictId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="level" label="* 能级">
            <el-select v-model="data.level" size="large" class="w_full"
                       placeholder="请选择能级">
              <el-option
                  v-for="(qtType, index) in data.listLevel"
                  :key="index"
                  :label="qtType.dictName"
                  :value="qtType.dictId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="college" label="* 毕业院校">
            <el-input size="large" placeholder="请输入" v-model="data.college"></el-input>
          </el-form-item>
          <el-form-item prop="identityCard" label="* 身份证号">
            <el-input size="large" placeholder="请输入" v-model="data.identityCard"></el-input>
          </el-form-item>

        </el-form>
      </div>
      <div class="flex_1" style="margin-left: 10px">
        <el-form label-position="top"
                 :model="data"
                 ref="form1"
                 style="margin: 10px; width: auto"
        >

          <el-form-item prop="gender" label="* 性别">
            <el-select v-model="data.gender" size="large" class="w_full"
                       placeholder="请选择性别">
              <el-option
                  v-for="(qtType, index) in data.listGender"
                  :key="index"
                  :label="qtType.value"
                  :value="qtType.key"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="userPassword" label="* 密码">
            <el-input size="large" placeholder="请输入" v-model="data.userPassword"></el-input>
          </el-form-item>
          <el-form-item prop="title" label="* 职称">
            <el-select v-model="data.title" size="large" class="w_full"
                       placeholder="请选择职称">
              <el-option
                  v-for="(qtType, index) in data.listTitle"
                  :key="index"
                  :label="qtType.dictName"
                  :value="qtType.dictId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="phoneNum" label="* 电话">
            <el-input size="large" placeholder="请输入" v-model="data.phoneNum"></el-input>
          </el-form-item>
          <el-form-item prop="education" label="* 教育背景">
            <el-input size="large" placeholder="请输入" v-model="data.education"></el-input>
          </el-form-item>
          <el-form-item prop="workTimeStr" label="* 参加工作时间">
            <el-date-picker v-model="data.workTimeStr"
                            type="date"
                            placeholder="请输入日期"
                            value-format="YYYY-MM-DD"
                            format="YYYY-MM-DD"
                            size="large"
                            class="w_full"
                            style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-form>

      </div>


    </div>
    <div class="w_full flex-row-center center-center" style="margin-top: 32px;">
      <el-button size="large" type="primary" @click="click_ok">确定</el-button>
      <el-button size="large" @click="click_cancel">取消</el-button>
    </div>
  </div>

</template>

<script setup lang="ts">
import {useUserStore} from "@/store/user";
import {onMounted, reactive} from "vue";
import msg from '@/components/msg'
import {UserInfoVO} from "@/type/vo/UserInfoVO";
import {timeStrAsTick} from "@/utils/TimeUtil";
import {api_getListDict} from "@/request/apis/assistantApi";
import {DICT_TYPE_LEVEL, DICT_TYPE_POSITION, DICT_TYPE_TITLE} from "@/constants/Constants_DictType";
import {DictInfoVO} from "@/type/vo/DictInfoVO";
import {api_addUser, api_editUser} from "@/request/apis/userApi";
import md5 from "md5";
import {ListGender} from "@/constants/Constants_ListGender";

const userStore = useUserStore()

const props = defineProps({
  userInfo: {
    type: UserInfoVO,
  },
  editFlag: {
    type: Boolean,
  },
});

// console.log('props',props.userInfo);
// console.log('props',props.editFlag);
const isEdit = props.userInfo && props.editFlag;
const emit = defineEmits(["close"])
// const form = ref<FormInstance>();

const data = reactive({
      userName: '',
      userPassword: '',
      userId: '',
      position: '',//岗位
      level: '',//岗位
      title: '',//岗位
      phoneNum: '',
      college: '',
      education: '',//
      identityCard: '',
      workTimeStr: '',
      gender: '',
      listTitle: Array<DictInfoVO>(),
      listLevel: Array<DictInfoVO>(),
      listPosition: Array<DictInfoVO>(),
      listGender: ListGender
    }
);

onMounted(async () => {
  await queryDict(DICT_TYPE_TITLE);
  await queryDict(DICT_TYPE_LEVEL);
  await queryDict(DICT_TYPE_POSITION);
  if (isEdit && props.userInfo) {
    data.userName = props.userInfo.userName
    // data.userPassword =md5(props.userInfo.userPassword)
    data.userId = props.userInfo.userId
    data.position = props.userInfo.position
    data.level = props.userInfo.workLevel
    data.title = props.userInfo.title
    data.gender = props.userInfo.gender
    data.phoneNum = props.userInfo.phoneNum
    data.college = props.userInfo.college
    data.education = props.userInfo.education
    data.identityCard = props.userInfo.identityCard
    data.workTimeStr = props.userInfo.workingTimeStr
    // console.log('data', data);
  }
})
const queryDict = async (dictType: string) => {
  let r = await api_getListDict(dictType);
  if (!r) {
    await msg.error("数据异常");
    return;
  }
  if (r.code != 0) {
    await msg.error(`数据异常${r.code} msg：${r.msg}`);
    return;
  }
  switch (dictType) {
    case DICT_TYPE_TITLE:
      data.listTitle = r.data;
      break;
    case DICT_TYPE_LEVEL:
      data.listLevel = r.data;
      break;
    case DICT_TYPE_POSITION:
      data.listPosition = r.data;
      break;
  }
}
const click_cancel = () => {
  emit("close", false);
}
const click_ok = async () => {
  if (!data.userName) {
    await msg.info('请输入名称');
    return;
  }
  let workTime = data.workTimeStr ? timeStrAsTick(data.workTimeStr) : 0;

  let userInfo: UserInfoVO = new UserInfoVO();
  if (isEdit) {
    userInfo = props.userInfo;
  }
  userInfo.userName = data.userName
  if (isEdit) {
    if (data.userPassword.length == 0) {
      userInfo.userPassword = props.userInfo.userPassword
    } else {
      userInfo.userPassword = md5(data.userPassword)
    }
  } else {
    userInfo.userPassword = md5(data.userPassword)
  }

  userInfo.userId = data.userId
  userInfo.position = data.position
  userInfo.workLevel = data.level
  userInfo.title = data.title
  userInfo.gender = data.gender
  userInfo.phoneNum = data.phoneNum
  userInfo.college = data.college
  userInfo.education = data.education
  userInfo.identityCard = data.identityCard
  userInfo.workingDate = workTime;
  const r = await (isEdit ? api_editUser(userInfo) : api_addUser(userInfo));
  if (!r) {
    await msg.error("数据异常");
    return;
  }
  if (r.code != 0) {
    await msg.error(`数据异常!  ${r.code} msg：${r.msg}`);
    return;
  }
  const examId = r.data;
  //这里调用创建接口
  emit("close", true, examId);
}
</script>

<style scoped>

</style>